<?php
namespace app\common\controller;
use app\common\model\Agent;
use app\common\model\WxOpen;
use app\common\model\Merchant;
use app\common\model\System;
use think\Controller;
use Wechat\Wxobjopen;
use think\Session;
class Upload extends Controller
{

    //缩略图上传
    public function uploadcard(){
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/card/');
        if($info){
            $imgurl['pic'] = '/uploads/card/'.$info->getSaveName();
            $imgurl['name'] = $info->getSaveName();
            $imgurl['ext'] = $info->getExtension();
//            $other = $this->uploadwx($imgurl['pic']);
//            $data['other'] = $other['url'];
//            $data['oth'] = $other;
            return json($imgurl);
        }else{
            return json($file->getError());
        }
    }

    //excel上传
    public function uploadexcel(){
        $file = request()->file('excel');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['ext'=>'xls,xlxs'])->move(ROOT_PATH . 'public' . DS . 'uploads/excel/');
        if ($info){
            $excel['url'] =  ROOT_PATH . 'public' . DS . 'excel' . DS . $info->getSaveName();
            return json($excel);
        }else{
            return json($file->getError());
        }
    }

    //微信上传
    public function uploadwx($img){
        $data = array("buffer" => '@.'.$img);
        $option = $this->option();
        $wxObj = new Wxobjopen($option);
        $res = $wxObj->uploadImg($data);
        return $res;
    }

    //微信API参数
    public function option(){
        Session::set('openid', null);
        $map['id'] = 1;
        $Merchant=new Merchant();
        $Agent=new Agent();
        $System=new System();
        $WxOpen=new WxOpen();
        $user = $Merchant->where($map)->find();
        $agent = $Agent->where(array('id' => $user['agent']))->find();
        $config = $System->where(array('id' => 1))->find();
        $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->find();
        $options = array(
            'component_token' => $agent['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $agent['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $agent['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $agent['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $agent['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }
}
